    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servicios.Dao.Impl;

import Entidades.Organo;
import Servicios.Conexion;
import Servicios.Dao.OrganoDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TRACY
 */
public class OrganoDaoImpl implements OrganoDao{
    Connection cn;
    
    public OrganoDaoImpl(Connection cn){
        this.cn = cn;
    }

    @Override
    public List<Organo> buscarOrganos() {
        List<Organo> organos = new ArrayList<Organo>();
        String sql ="select * from ORGANO";
        try {
            PreparedStatement pst = cn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {                
                Organo organo = new Organo();
                organo.setDepartamento(rs.getString("DEPARTAMENTO"));
                organo.setDireccion(rs.getString("DIRECCION"));
                organo.setDistrito(rs.getString("DISTRITO"));
                organo.setIDOrgano(rs.getString("IDORGANO"));
                organo.setProvincia(rs.getString("PROVINCIA"));
                organo.setTipo(rs.getString("TIPO"));
                organo.setNombreOrgano(rs.getString("NOMBREORGANO"));
                organos.add(organo);
            }
        } catch (SQLException ex) {
            Logger.getLogger(OrganoDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return organos;
    }

    @Override
    public Organo buscarOrgano(String nombre) {
        Organo organo = new Organo();
        String sql = "select * from ORGANO where NOMBREORGANO = ?";
        try {
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setString(1, nombre);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {                
                organo.setDepartamento(rs.getString("DEPARTAMENTO"));
                organo.setDireccion(rs.getString("DIRECCION"));
                organo.setDistrito(rs.getString("DISTRITO"));
                organo.setIDOrgano(rs.getString("IDORGANO"));
                organo.setProvincia(rs.getString("PROVINCIA"));
                organo.setTipo(rs.getString("TIPO"));
                organo.setNombreOrgano(rs.getString("NOMBREORGANO"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(OrganoDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return organo;
    }

    @Override
    public Organo buscarOrganoxID(String id) {
        Organo organo = new Organo();
        String sql = "select * from ORGANO where IDORGANO = ?";
        try {
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setString(1, id);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {                
                organo.setDepartamento(rs.getString("DEPARTAMENTO"));
                organo.setDireccion(rs.getString("DIRECCION"));
                organo.setDistrito(rs.getString("DISTRITO"));
                organo.setIDOrgano(rs.getString("IDORGANO"));
                organo.setProvincia(rs.getString("PROVINCIA"));
                organo.setTipo(rs.getString("TIPO"));
                organo.setNombreOrgano(rs.getString("NOMBREORGANO"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(OrganoDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return organo;
    }
    
}
